import { JSONMapping } from "../json-mapping";
import { UserDto } from "../dto/user.dto";
import {ObjectDto, ObjectDtoInterface} from "../../services/sqliteDB/object.dto";
import { Bill } from "./bill";
import {WorkOrder, WorkOrderStatus} from "./work-order";
import {SyncStatus} from "../sync-status";

export class User extends JSONMapping implements ObjectDtoInterface {

  static PropertyKeyToJSONKeyMap() {
    return {
      fullName: "customerName",
      telephone: "customerTelephone",
      mobile: "customerMobile",
      address: "customerAddress",
      id: "customerNo",
      bills: "payments|arrears",
      account: "account|customerAccount",
      theToppestMonth: "maxChargeMonth|theToppestMonth"
    };
  }

  static JSONValueTranslator(propertyKey) {
    if (propertyKey === "bills") {
      return (billJsonObjs: Array<{[key: string]: any}>) => {
        return billJsonObjs ? billJsonObjs.map((billJson) => new Bill(billJson)) : null;
      };
    }
    return null;
  }

  public id: string = null;
  public fullName: string = null;
  public account: string = null;
  public telephone: string = null;
  public mobile: string = null;
  public address: string = null;
  public areaId: string = null; // 区域号
  public districtId: string = null; // 台号
  public propertyNo: string = null; // 资产编号

  public totalUnPaidAmount: number = null; // 欠费总额
  public theToppestChargeLevel: number = null; // 欠费最高的催费级别
  public chargeDetailIds: string = null; // 欠费订单的chargeDetailIds
  public theToppestMonth: number = null; // 欠费最大的月份
  public maxChargeOperationStatus: WorkOrderStatus = null;
  public noticeUploadTime: number = null;
  public bills: Array<Bill> = null;
  public workOrders: Array<WorkOrder> = null;
  public syncStatus: SyncStatus = SyncStatus.None;

  constructor(json: { [key: string]: any }) {
    super(json);
    User.mapFromJSON(json, this);
  }

  get unPaidBills(): Array<Bill> {
    if (!this.bills) {
      return [];
    }
    return this.bills.reduce((prev, current) => {
      if (!current.isSettle) {
        prev.push(current);
      }
      return prev;
    }, []);
  }

  get paidBills(): Array<Bill> {
    if (!this.bills) {
      return [];
    }
    return this.bills.reduce((prev, current) => {
      if (current.isSettle) {
        prev.push(current);
      }
      return prev;
    }, []);
  }



}

